home *** CD-ROM | disk | FTP | other *** search
/ Gigarom 1 / Gigarom Macintosh Archives (Quantum Leap)(CDRM1080320)(1993).iso / FILES / BBS / MUBBS / New Msgs.cpt / New msgs / New Messages Module.c next >
C/C++ Source or Header  |  1992-03-09  |  6KB  |  215 lines

  1. /*
  2.  *   New Messages Module - by Myles Wakeham
  3.  *
  4.  *      (C)1991 Myles Wakeham - Please see the file "COPYRITE.MGW"
  5.  *
  6.  *        ))) Version Changes (((
  7.  * --Version-------------Notes-----------------------------------------------------------------
  8.  *  Initial                Version based on Discussions in V1.1 MUBBS              
  9.  *
  10.  *  This program may not be sold for profit!
  11.  *  The source code is being release for educational purposes only.
  12.  *  I take no responsibility for any damage resulting from this code.
  13.  */
  14.  
  15. #define INMAIN
  16. #include    "MUBBS Module.h"
  17. #include     <SetUpA4.h>
  18.  
  19. #define NOLINES 7
  20. #define DISTMAX 50
  21. #define LEN 80
  22.  
  23. pascal void main(int mode1, struct GS *G1, Ptr P1);
  24. int NewMessages(void);
  25. int count_recs(char filename[]);
  26.  
  27.  
  28.  
  29. pascal void main (mode1,G1,P1) /* called from the main routines, and what mode to be in */
  30. int mode1;
  31. struct GS *G1; /* we point to the "global" struct in the Main Module here */
  32. Ptr P1; /* we ignore this pointer, we do not use it at all */
  33. {
  34. Handle temph;
  35. float version = 1.0; /* what version of MUBBS you are compatable with IE: 1.0 and above
  36.                         MAKE SURE YOU ARE COMPATABLE BEFORE SETTING THIS LESS THAN THE
  37.                         VERSION OF MUBBS YOU ARE DEVELOPING THIS FOR ! */
  38.  
  39. RememberA0(); SetUpA4(); /* This sets up the A4 register to access our globals */
  40. asm { _RecoverHandle }; asm {move.l a0,temph}; HLock(temph); /* locks our module, do this ! */
  41.  
  42. G=G1; /* This MUST be the first thing you do in main only, it sets up the struct globals */
  43. mode[u]=mode1; /* set up our mode so that you can read it anywhere */
  44.  
  45. switch (mode[u]) { /* any un-handled modes return error from this module */
  46.             /* modes are
  47.                0= Return the programmers name and the module's type & version only.
  48.                1= Bye bye call, if you returned 99 for a init call, you
  49.                   get called when the program quits with mode=1.
  50.                   You should do a "unlock" to release your module's memory space
  51.                   and unlock any memory you allocated.
  52.                2= Normal call, no pointer passed.
  53.                3= Call with a pointer to a data struct (for passing data to a module).
  54.               98= do a version check against MUBBS version.
  55.             */
  56.  
  57.     case 2:
  58.         NewMessages();
  59.         G->moduleresult=0;
  60.         break;
  61.     case 98:
  62.         versionck(version); /* just return after this call, don't modify anything */
  63.         break;        
  64.     case 0:
  65.         strcpy (G->programmer,"Myles Wakeham"); /* show the programmer's name up to 20 chars*/
  66.         strcpy (G->modtype,"New Msg V1.0");     /* module type & version up to 30 chars
  67.                                                the TYPE isn't always the name! */
  68.         G->moduleresult=0; /* if this was also a init call we need close call put 99 here */
  69.         break;
  70.     default:
  71.         G->moduleresult=1; /* return bad code */
  72.     };
  73.  
  74. HUnlock(temph); /* unlocks this module, do this ! */
  75. RestoreA4(); /* call this when you are all done */
  76. }
  77.  
  78. /****        *   *    *    -NewMessages()-   *   *   *   *          ****/
  79.  
  80. NewMessages()  
  81. {
  82.     FILE    *stream1, *stream2;
  83.     char    str[80];
  84.     char    Myusername[LEN];
  85.     char    filename[65];
  86.     char    msgcountfile[LEN];
  87.     char    left_half[LEN];
  88.     char    msgfile[LEN];
  89.     static     char distgroups[DISTMAX] [LEN];
  90.     int        count;
  91.     int        firsttime;
  92.     int        lastcount;
  93.     int        msgsread;
  94.     int        linecount;
  95.     int        actualmsgs;
  96.     
  97.     if (!G->online[u]) return; /* Log out if hang up */
  98.     loguser(G->modulename[u]); /* write to log file */
  99.     strcpy(filename,":Dis:");
  100.     strcat(filename,G->modulename[u]);
  101.     strcat(filename,".intro");
  102.     print("C> Line %d %s, at: %s\n",(u+1),G->username[u],G->modulename[u]);
  103.     send (G->CR[u]);
  104.     sendtext(filename);
  105.     
  106.     firsttime=TRUE;
  107.  
  108.     /* Now intialize the distgroups array */
  109.     for(count=0;count<=DISTMAX-1;count++)
  110.         distgroups[count][0]='\0';
  111.                 
  112.     /* Load the distgroups array with the content of distlist */
  113.     if ((stream1 = fopen(":bbssupport:dislist","r")) == NULL)
  114.     {
  115.         send("]ERROR -43 Cannot open :bbssupport:dislist]");
  116.         print("ERROR - Cannot open :bbssupport:dislist \n");
  117.     }
  118.     else
  119.     {    
  120.         for(count=0;count<=DISTMAX-1;count++) {
  121.             if (fscanf(stream1,"%80[^\n]\n",distgroups[count]) == EOF) break; /* end on EOF */
  122.             }
  123.  
  124.         /* Save the Count variable */
  125.         lastcount=count;
  126.         fclose(stream1);
  127.                 
  128.         for(count=0;count<=lastcount-1;count++)
  129.         {
  130.             /* Establish Message Count File Name */
  131.             strcpy(msgcountfile,":Dis:");
  132.             strcpy(Myusername,G->username[u]);
  133.             strcat(msgcountfile,Myusername);
  134.             strcat(msgcountfile,distgroups[count]); 
  135.             strcat(msgcountfile,".data");
  136.                     
  137.             /* Establish Messages file name */
  138.             strcpy(msgfile,":Dis:");
  139.             strcat(msgfile,distgroups[count]); 
  140.             strcat(msgfile,".text");
  141.                     
  142.             /* Now open the files and check for new records */
  143.             if ((stream2 = fopen(msgcountfile,"r")) == NULL)
  144.                 msgsread=0;
  145.             else
  146.             {
  147.                 if(fgets(str,80,stream2) != NULL)
  148.                 {
  149.                     sscanf(str,"%d",&msgsread);    
  150.                 }
  151.                 fclose(stream2);
  152.             }    
  153.                                         
  154.             /* Count the number of messages in the discussions files */
  155.             actualmsgs = count_recs(msgfile);
  156.             if(actualmsgs>=0)
  157.             {
  158.                 /* Check the actual number of messages with the */
  159.                 /* number of messages read and report new messages */
  160.                                     
  161.                 if(actualmsgs>msgsread)
  162.                 {
  163.                     if(firsttime)
  164.                     {
  165.                         send("]Here's the messages you haven't read...]]");
  166.                         firsttime=FALSE;
  167.                     }
  168.  
  169.                     if(actualmsgs-msgsread<2)
  170.                         send("There is %d new message in %s]",
  171.                             actualmsgs-msgsread, distgroups[count]);
  172.                     else
  173.                         send("There are %d new messages in %s]",
  174.                             actualmsgs-msgsread, distgroups[count]);
  175.                 }
  176.             }
  177.         }
  178.         send("]]End of List.]]");
  179.     }
  180. }
  181.  
  182.  
  183. int count_recs(filename)
  184.     char    filename[];
  185. {
  186.     FILE    *stream10;
  187.     int        rec_count;
  188.     char    str1[81];
  189.     char    squiglyStr[81];
  190.     int        p;
  191.     
  192.     /* Open the file and count the records */
  193.     strcpy(squiglyStr,"~\n");
  194.     if ((stream10 = fopen(filename,"r")) == NULL)
  195.     {
  196.         rec_count=(-1);
  197.     }
  198.     else
  199.     {
  200.         rec_count=0;
  201.         while(fgets(str1,80,stream10) != NULL)
  202.         {
  203.             if (strcmp(str1,squiglyStr)==0)
  204.             {
  205.                 rec_count++;
  206.             }
  207.         otheruser(FALSE);
  208.         }
  209.         fclose(stream10);
  210.     }
  211.     return(rec_count);
  212. }
  213.  
  214.  
  215.